import { computed, defineComponent, createVNode } from "vue";
import Checkbox from "../checkbox";

export default defineComponent({
  name: "selectionBox",
  inheritAttrs: false,
  props: {
    // 默认选中项
    defaultSelection: {},
    // 选中行数据
    selectedRows: {
      type: Array,
      default: () => [],
    },
    selectedRowKeys: {
      type: Array,
      default: () => [],
    },
    // 当前行数据
    row: {
      type: Object,
      default: () => {},
    },
    rowKey: {},
    type: {
      type: String,
      default: "checkbox",
    },
    name: {
      type: String,
      default: "",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
  },

  setup: function setup(props, context) {
    return {
      checked: computed(function () {
        let defaultSelection = props.defaultSelection,
          selectedRows = props.selectedRows,
          selectedRowKeys = props.selectedRowKeys,
          row = props.row;

        let checked = selectedRowKeys.includes(row.key);

        return checked;
      }),
    };
  },

  render() {
    let checked = this.checked;

    return createVNode(
      Checkbox,
      {
        checked,
        iconSize: "16px",
        shape: "square",
        onClick: this.$attrs.onChange,
        name: this.name,
        disabled: this.disabled,
      },
      null
    );
  },
});
